<?php

namespace App\Http\Controllers\Admin;

use App\Common\ApiErrorCode;
use App\Http\Controllers\Controller;
use App\Models\Admin;
use App\Models\Rbac\Role;
use Illuminate\Http\Request;

class AdminController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index(Request $request)
    {
        $params = $request->only('title','nickname','username','phone','status');
        $res = Admin::getQueryData($params);
        foreach ($res as $admin){
            $admin->role_ids = $admin->roles->pluck('id')->toArray();
            $admin->role_names = $admin->roles->pluck('cn_name')->toArray();
            unset($admin->roles);
        }
        return apiResponse(data: $res);
    }

    public function getOptions()
    {
        $roles = Role::query()->select('id', 'name','cn_name')->get();
        return apiResponse(data: [
            'roles' => $roles
        ]);
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $request->validate([
            'username' => 'bail|required|string|min:4|max:30|unique:admins',
            'password' => 'bail|required|string|min:6|max:50',
        ]);
        $params = $request->all();
        $params['register_ip'] = $request->getClientIp();
        $admin = Admin::create($params);
        if (isset($params['role_ids'])){
            $roles = Role::whereIn('id', $params['role_ids'])->get();
            $admin->syncRoles($roles);
//            $admin->roles()->attach($params['role_ids'],['created_at' => now()]);
        }
        return apiResponse(data: $admin);
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        $res = Admin::query()->findOrFail($id);
        return apiResponse(data: $res);
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id)
    {
        $request->validate([
            'username' => 'bail|filled|string|min:4|max:30',
            'password' => 'bail|filled|string|min:6|max:50',
        ]);
        $params = $request->all();
        $admin = Admin::query()->findOrFail($id);
        unset($params['username']);
        $admin->update($params);
        if (isset($params['role_ids'])){
            $roles = Role::whereIn('id', $params['role_ids'])->get();
            $admin->syncRoles($roles);
        }
//        $admin->roles()->sync(array_fill_keys($params['role_ids'] ?? [], ['updated_at' => now()]));
        return apiResponse(data: $params);
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id)
    {
        $admin = Admin::query()->findOrFail($id);
        if($admin->username == 'admin'){
            return apiResponseError(ApiErrorCode::ADMIN_NOT_DEL);
        }
        $admin->delete();
        return apiResponse();
    }

    /**
     * 修改个人信息
     */
    public function updateProfile(Request $request)
    {
        $validated = $request->validate([
            'avatar' => 'bail|required|string|max:255',
            'nickname' => 'bail|required|string|max:255',
            'phone' => 'bail|sometimes|string|max:255',
            'email' => 'bail|sometimes|email|max:255'
        ]);
        $admin = auth('admin')->user();
        $admin->update($validated);
        return apiResponse(data: $validated);
    }
}
